<?php
/**
 * Created by PhpStorm.
 * User: 戎大富
 * Date: 2020/10/15
 * Time: 11:19
 */
namespace app\admin\controller;
use think\Db;
use think\Request;
class Vip extends Base {
    /**
     * 列表展示
     * @return mixed|\think\response\Json
     */
    public function index(){
        if(request()->isAjax()){
            $map = array();
            extract(input());
            if(isset($keyword)&&$keyword!="")
            {
                $map['vip_title'] = ['like',"%" . $keyword . "%"];
            }
            if(isset($start) && $start != '' && isset($end) && $end != ''){
                $map['vip_time'] = ['between', [$start, $end]];
            }
            if(isset($start) && $start != '' && isset($end) && $end == ''){
                $map['vip_time'] = ['> time', $start];
            }
            if(isset($start) && $start == '' && isset($end) && $end != ''){
                $map['vip_time'] = ['< time', $end];
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::name('vip')
                ->where($map)->count();// 获取总条数
            $lists = Db::name('vip')
                ->where($map)
                ->page($NowPage,$limits)
                ->order('vip_time desc')
                ->select();
            foreach ($lists as $key=>&$vo){
                if($vo['vip_seat']==1){
                    $vo['vip_seat_name'] ='金卡';
                }elseif ($vo['vip_seat']==2){
                    $vo['vip_seat_name'] ='白金卡';
                }elseif ($vo['vip_seat']==3){
                    $vo['vip_seat_name'] ='黑卡';
                }
            }

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }
    /***
     * @return \think\response\Json
     * 删除
     */
    public function del(){
        $task_id = input('param.id');
        $res = Db::name('vip')->where('vip_id',$task_id)->delete();
        if($res){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>100,'msg'=>'删除失败']);
        }
    }
    /***
     * @return mixed|\think\response\Json
     * 添加
     */
    public function add(){
        if(Request::instance()->isPost()){
            $vip_title =input('vip_title');
            $vip_img= input('vip_img');
            $vip_seat = input('vip_seat');
            $vip_price = input('vip_price');
            $vip_shuoming = input('vip_shuoming');
            $data['vip_title'] = $vip_title;
            $data['vip_img'] = $vip_img;
            $data['vip_seat'] = $vip_seat;
            $data['vip_price'] = $vip_price;
            $data['vip_shuoming'] = $vip_shuoming;
            $data['vip_time'] = date('Y-m-d H:i:s',time());
            $res = Db::table('up_vip')->insert($data);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch();
    }

    /***
     * @return mixed|\think\response\Json
     * 编辑
     */
    public function update(){
        if(Request::instance()->isPost()){
            $id = input('id');
            $vip_title =input('vip_title');
            $vip_img= input('vip_img');
            $vip_seat = input('vip_seat');
            $vip_price = input('vip_price');
            $vip_shuoming = input('vip_shuoming');
            $data['vip_title'] = $vip_title;
            $data['vip_seat'] = $vip_seat;
            $data['vip_price'] = $vip_price;
            $data['vip_shuoming'] = $vip_shuoming;

            if(!empty($vip_img)){
                $data['vip_img'] = $vip_img;
            }
            $res = Db::table('up_vip')->where('vip_id',$id)->update($data);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $id = input('vip_id');
        $info= Db::table('up_vip')->where('vip_id',$id)->find();
        $this->assign('id',$id);
        $this->assign('res',$info);
        return $this->fetch();
    }
}